/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.compileunit.CompileUnitGenerator;
import com.inspur.edp.caf.generator.compileunit.CompileUnitGeneratorContext;
import com.inspur.edp.cef.api.attr.CefConfigAttribute;
import com.inspur.edp.cef.entity.config.CefConfig;
import com.inspur.edp.cef.generator.coreGenerator.base.configGenerator.BeanConfigClassGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.configGenerator.ConfigClassGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.ManagerGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;
import com.inspur.edp.das.commonmodel.entity.GspCommonModel;
import java.util.HashMap;

import com.inspur.edp.cef.generator.repository.RepositoryGenerator;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.TypeLiteral;


public abstract class CefCoreGenerator extends CompileUnitGenerator {

  private CefGeneratorContext cefGenContext;

  protected CefGeneratorContext getCefGenContext() {
    return cefGenContext;
  }

  protected CefCoreGenerator(CefGeneratorContext cefGenContext) {
    this.cefGenContext = cefGenContext;
  }

  // region Context

  // keyword:
  // ORIGINAL LINE: protected new CefCoreGeneratorContext getContext() { return
  // (CefCoreGeneratorContext)super.getContext(); }
  @Override
  protected CefCoreGeneratorContext getContext() {
    return (CefCoreGeneratorContext) super.getContext();
  }

  @Override
  protected CompileUnitGeneratorContext createProjectInfoContext() {
    CefCoreGeneratorContext coreCtx = createCoreGenContext();
    cefGenContext.setCoreGenContext(coreCtx);
    cefGenContext.setCefCoreGenCtx(coreCtx);
    return coreCtx;
  }

  protected abstract CefCoreGeneratorContext createCoreGenContext();
  // endregion

  @Override
  protected String getPackageName() {
    return getContext().getNameSpace();
  }

  @Override
  protected java.util.ArrayList<BaseGenerator> createChildGenerators() {
    java.util.ArrayList<BaseGenerator> childGenerators = super.createChildGenerators();
    if (childGenerators == null) {
      childGenerators = new java.util.ArrayList<BaseGenerator>();
    }
    //childGenerators.add(createDataTypeImplGenerator());
    childGenerators.add(createMgrImplGenerator());
    if (getHasConfigGenerator()) {
      ConfigClassGenerator configGenerator = getConfigGenerator();
      childGenerators.add(configGenerator);
      BeanConfigClassGenerator beanConfigClassGenerator = getConfigBeanGenerator();
      childGenerators.add(beanConfigClassGenerator);
      AnnotationInfo tempVar = new AnnotationInfo();
      tempVar.setTypeInfo(Utils.createTypeInfo(CefConfigAttribute.class));
      tempVar.setAnnotationType(AnnotationType.Normal);
      HashMap<String, Expression> annoParams = new HashMap<String, Expression>();
      TypeLiteral typeLiteral = ast.newTypeLiteral();
      typeLiteral.setType(ast.newSimpleType(ast.newName(configGenerator.getFullName())));
      annoParams.put("configType", typeLiteral);
      tempVar.setParams(annoParams);
      //TODO: java版临时注释 包上打标签待验证
//      this.getContext().addAttribute(tempVar);
    } else {
      CefConfig config = buildConfigInfo();
      getContext().setConfig(config);
      //IGspCommonModel commonModel = ((CommonModelCoreGeneratorContext) getContext()).getCommonModel();
    }
    if (getHasRepository()) {
      RepositoryGenerator repoGen = getRepositoryGenerator();
      childGenerators.add(repoGen);
    }
    return childGenerators;
  }

  protected CefConfig buildConfigInfo() {
    return null;
  }

  protected RepositoryGenerator getRepositoryGenerator() {
    return null;
  }
  protected boolean getHasRepository() {
    return false;
  }

  protected boolean getHasConfigGenerator() {
    return false;
  }

  protected ConfigClassGenerator getConfigGenerator() {
    throw new UnsupportedOperationException();
  }

  protected BeanConfigClassGenerator getConfigBeanGenerator() {
    return new BeanConfigClassGenerator(getContext());
  }

//  protected abstract CefDataTypeImplGenerator createDataTypeImplGenerator();

  protected abstract ManagerGenerator createMgrImplGenerator();

  //	@Override
  //	protected java.util.ArrayList<Assembly>  addReferences()
  //	{

  //		var list = super.addReferences();
  //		if (list == null)
  //		{
  //			list = new java.util.ArrayList<Assembly>();
  //		}
  //		//list.add(cefGenContext.ApiAssembly);
  //		//list.add(CefGenContext.EntityAssembly);
  //		return list;
  //	}

  @Override
  protected String getFilePath() {
    return cefGenContext.getMetadataJitContext().getCodePath();
  }

  //	@Override
  //	protected GenerateType  getGenerateType()
  //	{
  //		return GenerateType.LocalFile;
  //	}

  @Override
  protected void doInitialize() {
    super.doInitialize();
    // Context.Assembly = ReflectionUtil.GetAssemblyFromPath(cefGenContext.GeneratePath,
    // getAssemblyName());
    // beGenCtx.BEInterfaceType = Context.Assembly.GetType(Context.BEInterfaceType.getFullName());
    cefGenContext.setMgrImplType(
        getContext()
            .getMgrImplType()); // Context.Assembly.GetType(Context.MgrImplType.getFullName());
    cefGenContext.setMgrAssemblyName(getPackageName());
  }

//  @Override
//  protected void beforeGenerate()
//  {
//    super.beforeGenerate();
//    cefGenContext.getMetadataJitContext().getCoreModuleGenerator().addCompileUnitGenrator(this);
//  }
}
